#include <iostream>
#include <vector>
#include <cstdio> 

#define in read()
#define fi first
#define se second
#define pb push_back

using namespace std;

typedef long long ll;
typedef double db;
typedef vector < int >  vec;
typedef pair < int , int > pii;

int read() {
  int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-',ch = getchar();
  while(isdigit(ch)) x = x * 10 + (ch ^ 48),ch = getchar(); return f ? -x : x;
}

const int N = 1e5 + 10;
const int inf = 0x3f3f3f3f;

int tg[N],val1[N],val2[N],tim,n,m;
ll ans;
struct mnval {
    int c,d,p;
} mn[N];

void up(int x,int y) {
    int d = mn[y].d + (val1[y] ? -1 : 1);
    if(d < mn[x].d) mn[x].d = d,mn[x].p = mn[y].p;
}

void pullup(int x) {
    mn[x].d = inf;
    if((x << 1) <= n) up(x,x << 1); if((x << 1) < n) up(x,x << 1 | 1);
    if(mn[x].c && mn[x].d > 0) mn[x].d = 0,mn[x].p = x;
}

void build(int x) {
    if((x << 1) <= n) build(x << 1); if((x << 1) < n) build(x << 1 | 1);
    pullup(x);
}

void gettag(int x) { while(x) tg[x] = tim,x >>= 1; }
void upd1(int x) { while(x && tg[x] != tim) (val2[x] ? val2[x]-- : val1[x]++),x >>= 1; }
void upd2(int x) { while(x && tg[x] != tim) (val1[x] ? val1[x]-- : val2[x]++),x >>= 1; } // up flow
void downup(int x) { while(x) pullup(x),x >>= 1; }

int main() {
#ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
#endif
    n = in,m = in; for(int i = 1;i <= n;i++) mn[i].c = in; build(1);
    for(int i = 1;i <= m;i++) {
	int x = in,cur = x,pos = 0,mndis = inf,dis = 0;
	while(cur) {
	    if(dis + mn[cur].d < mndis) mndis = mn[cur].d + dis,pos = mn[cur].p;
	    dis += val2[cur] ? -1 : 1; cur >>= 1;
	}
	mn[pos].c--; ++tim; gettag(x); upd2(pos);
	++tim; gettag(pos); upd1(x);
	downup(x); downup(pos);
	ans += mndis; printf("%lld ",ans);
    } puts(""); return 0;
} 
